<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>FiletreeTest</title>
</head>
<body>
	
</body>
</html>
<script src="<{$root}>codeAdmin/Public/js/jquery-1.7.2.min.js"></script>
<script>
	function write_root (item) {
		$(item).append('<ul class="tree treeFolder collapse" ></ul>');
		return $(item).children("ul");
	}
	function write_dir(item,dir_name){
		if(0==arguments.length){
			throw "2 args needed";
		}else if (1==arguments.length) {
			dir_name="未知文件夹";
		}
		var li=document.createElement("li");
		var a=document.createElement("a");
		a.innerHTML=dir_name;
		var ul=document.createElement("ul");
		li.appendChild(a);
		li.appendChild(ul);
		item[0].appendChild(li);
		return $(ul);
	}
	function write_file(item,file_name,url) {
		if(0==arguments.length){
			throw "2 args needed";
		}else if (1==arguments.length) {
			dir_name="未知文件";
		}
		var li=document.createElement("li");
		var a=document.createElement("a");
		a.innerHTML=file_name;
		a.href=url;
		li.appendChild(a);
		item[0].appendChild(li);
		// $(item).append('<li><a href="#">'+file_name+'</a></li>');
		// return $(item).children("li");
		return $(li);
	}
	function tree_construct(base,json) {
		for (var e in json) {
			if (!isNaN(e)) {
				write_file(base,json[e],json[e]);//TODO there create url
			} else{
				var son_dir=write_dir(base,e);
				tree_construct(son_dir,json[e]);
			};
		};
	}

	$(document).ready(function() {
		var json_dir = $.ajax({
		  url: "<{:U("filetree/dir")}>",
		  async: false
		 }).responseText;
		var root=write_root($("body"));
		var json_dir=eval("["+json_dir+"]")[0];
		// console.log(json_dir);
		tree_construct(root,json_dir);
	});
</script>